import request from '@/utils/request';

// 获取交易记录列表（分页）
export const getTransactions = async (params) => {
  // 清空params中的null和undefined值
  Object.keys(params).forEach(key => {
    if (params[key] === null || params[key] === undefined) {
      delete params[key];
    }
  });

  return request({
    url: '/transactions',
    method: 'get',
    params
  });
};

// 获取交易记录详情（与getTransactionById保持一致）
export const getTransactionDetail = async (id) => {
  return request({
    url: `/transactions/${id}`,
    method: 'get'
  });
};

// 别名，与原有API保持兼容性
export const getTransactionById = getTransactionDetail;

// 创建交易记录
export const createTransaction = async (data) => {
  return request({
    url: '/transactions',
    method: 'post',
    data
  });
};

// 更新交易记录
export const updateTransaction = async (id, data) => {
  return request({
    url: `/transactions/${id}`,
    method: 'put',
    data
  });
};

// 删除交易记录
export const deleteTransaction = async (id) => {
  return request({
    url: `/transactions/${id}`,
    method: 'delete'
  });
};

// 批量删除交易记录
export const batchDeleteTransactions = async (ids) => {
  return request({
    url: '/transactions/batch',
    method: 'delete',
    data: ids
  });
};

// 别名，与原有API保持兼容性
export const deleteBatchTransactions = batchDeleteTransactions;

// 根据条件搜索交易记录
export const searchTransactions = async (params) => {
  return request({
    url: '/transactions/search',
    method: 'post',
    data: params
  });
};

// 获取按平台统计数据
export const getPlatformStats = async (params = {}) => {
  return request({
    url: '/transactions/stats/platform',
    method: 'get',
    params
  });
};

// 获取按收支类型统计数据
export const getIncomeExpenseStats = async (params = {}) => {
  return request({
    url: '/transactions/stats/income-expense',
    method: 'get',
    params
  });
};

// 获取按交易对象统计数据
export const getTransactionPartnerStats = async (params = {}) => {
  return request({
    url: '/transactions/stats/transaction-partner',
    method: 'get',
    params
  });
};

// 批量创建交易记录
export const createBatchTransactions = async (transactions) => {
  return request({
    url: '/transactions/batch',
    method: 'post',
    data: transactions
  });
};

// 根据支付平台查询
export const getTransactionsByPlatform = async (platform) => {
  return request({
    url: `/transactions/platform/${platform}`,
    method: 'get'
  });
};

// 根据收支类型查询
export const getTransactionsByIncomeExpense = async (type) => {
  return request({
    url: `/transactions/income-expense/${type}`,
    method: 'get'
  });
};

// 上传文件
export const uploadFile = async (formData) => {
  return request({
    url: '/transactions/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
};

// 导出统一的transactionService对象，便于使用
export const transactionService = {
  getTransactions,
  getTransactionById,
  getTransactionDetail,
  createTransaction,
  createBatchTransactions,
  updateTransaction,
  deleteTransaction,
  deleteBatchTransactions,
  batchDeleteTransactions,
  searchTransactions,
  getPlatformStats,
  getIncomeExpenseStats,
  getTransactionPartnerStats,
  getTransactionsByPlatform,
  getTransactionsByIncomeExpense,
  uploadFile
};